const data = [
  {deparment: '服体-太平洋-大模型'},
  {deparment: '服体-太平洋-门户'},
  {deparment: '服体-太平洋-基础平台'},
  {deparment: '服体-风控-风控小组1'},
  {deparment: '服体-风控-风控小组2'},
  {deparment: '金融-风控-风控小组1'},
  {deparment: '服体-风控-风控小组1'}
]

const nodeFactory = (name) => {
  return {
    id: new Date().getTime(),
    name,
    children: [],
  }
}

// 构造当前节点 & 并加入父节点children中
const treeBuilder = (data) => {
  // path => node
  const map = new Map();

  const root = nodeFactory('公司');

  const buildCore = (deparmentPath, layerNumber) => {

    const deparmentArr = deparmentPath.split('-');

    if(layerNumber > deparmentArr.length) return;

    const needAppendToRoot = layerNumber === 1;

    const currentPath = deparmentArr.slice(0, layerNumber).join('-');

    const currentName = deparmentArr[layerNumber - 1];

    const parentPath = deparmentArr.slice(0, layerNumber - 1).join('-');

    const parentNode = map.get(parentPath);

    if(!map.has(currentPath)) {
      const currentNode = nodeFactory(currentName);
      map.set(currentPath, currentNode);

      if(needAppendToRoot) {
        root.children = [...root.children, currentNode];
      } else {
        parentNode.children = [...parentNode.children, currentNode];
      }
    }

    buildCore(deparmentPath, layerNumber + 1);
  }

  data.forEach((deparmentInfo) => buildCore(deparmentInfo.deparment, 1));

  return root;
}

console.log(treeBuilder(data));